// jq入口
$(function () {
    BindAjaxEvent('#company-submit', UploadCompanyResource);
    BindAjaxEvent('#employee-submit', UploadEmployeeResource);
    BindAjaxEvent('#case-submit', UploadCaseResource);
    BindAjaxEvent('#hotel-submit', UploadHotelResource);
    BindAjaxEvent('#index-submit', UploadIndexResource);
    SetMultiple();
    SetOtherStyles();
})

// 设置一些样式
function SetOtherStyles() {
    $('#company-res-form input[name="img_index"]').attr('id', 'id_img_index_company');
    $('#employee-res-form input[name="img_index"]').attr('id', 'id_img_index_employee');
    $('input[type="file"]').removeClass('form-control').addClass('form-control-file');
    $('textarea').attr('rows', '1');
    $('input').addClass('my-input-half');
}

// 为ID绑定Ajax事件
function BindAjaxEvent(jqId, ajaxFunction) {
    $(jqId).click(ajaxFunction);
}

// 为表单设置多选
function SetMultiple() {
    $('#id_img_company').attr('multiple', 'True');
    $('#id_img_employee').attr('multiple', 'True');
    $('#id_img_case').attr('multiple', 'True');
    $('#id_index_img').attr('multiple', 'True');
}

function ResetInput() {
    $(':input').val('');
}

// 上传婚庆公司资源
function UploadCompanyResource() {
    $('#upload-msg-Modal').modal('show');
    const formData = new FormData($('#company-res-form')[0]);
    console.log(formData);
    $.ajax({
        url: "/personal/upload/resource/company/",
        type: "POST",
        dataType: "json",
        data: formData,
        cache: false,                      // 不缓存
        processData: false,                // jQuery不要去处理发送的数据
        contentType: false,
        success: function (res) {
            if (res.status) {
                $('#upload-msg-Modal').modal('hide');
                $('#upload-success-Modal').modal('show');
                ResetInput();
            } else {
                $('#upload-msg-Modal').modal('hide');
                $('#upload-failure-Modal').modal('show');
            }
        }
    })
}

// 上传外聘资源
function UploadEmployeeResource() {
    $('#upload-msg-Modal').modal('show');
    const formData = new FormData($('#employee-res-form')[0]);
    console.log(formData);
    $.ajax({
        url: "/personal/upload/resource/employee/",
        type: "POST",
        dataType: "json",
        data: formData,
        cache: false,                      // 不缓存
        processData: false,                // jQuery不要去处理发送的数据
        contentType: false,
        success: function (res) {
            if (res.status) {
                $('#upload-msg-Modal').modal('hide');
                $('#upload-success-Modal').modal('show');
                ResetInput();

            } else {
                $('#upload-msg-Modal').modal('hide');
                $('#upload-failure-Modal').modal('show');
            }
        }
    })
}

// 上传优秀案例资源
function UploadCaseResource() {
    $('#upload-msg-Modal').modal('show');
    const formData = new FormData($('#case-res-form')[0]);
    console.log(formData);
    $.ajax({
        url: "/personal/upload/resource/case/",
        type: "POST",
        dataType: "json",
        data: formData,
        cache: false,                      // 不缓存
        processData: false,                // jQuery不要去处理发送的数据
        contentType: false,
        success: function (res) {
            if (res.status) {
                $('#upload-msg-Modal').modal('hide');
                $('#upload-success-Modal').modal('show');
            } else {
                $('#upload-msg-Modal').modal('hide');
                $('#upload-failure-Modal').modal('show');
                console.log(res.errors);
            }
        }
    })
}

//酒店资源上传
function UploadHotelResource() {
    $('#upload-msg-Modal').modal('show');
    const formData = new FormData($('#hotel-res-form')[0]);
    console.log(formData);
    $.ajax({
        url: "/personal/upload/resource/hotel/",
        type: "POST",
        dataType: "json",
        data: formData,
        cache: false,                      // 不缓存
        processData: false,                // jQuery不要去处理发送的数据
        contentType: false,
        success: function (res) {
            if (res.status) {
                $('#upload-msg-Modal').modal('hide');
                $('#upload-success-Modal').modal('show');
                // window.location.reload();
            } else {
                $('#upload-msg-Modal').modal('hide');
                $('#upload-failure-Modal').modal('show');

            }
        }
    })
}

//主页资源上传
function UploadIndexResource() {
    $('#upload-msg-Modal').modal('show');
    const formData = new FormData($('#index-res-form')[0]);
    console.log(formData);
    $.ajax({
        url: "/personal/upload/resource/index/",
        type: "POST",
        dataType: "json",
        data: formData,
        cache: false,                      // 不缓存
        processData: false,                // jQuery不要去处理发送的数据
        contentType: false,
        success: function (res) {
            if (res.status) {
                $('#upload-msg-Modal').modal('hide');
                $('#upload-success-Modal').modal('show');
            } else {
                $('#upload-msg-Modal').modal('hide');
                $('#upload-failure-Modal').modal('show');
            }
        }
    })
}